#include <QtGui>
#include <QIcon>

#include "newsviewer.hpp"
#include "browserwindow.hpp"
#include "newssetting.hpp"
#include <fstream>
#include <string>

NewsViewer::NewsViewer(NewsSetting * newssetting,QWidget *parent):newssetting(newssetting),QDialog(parent){
    setupUi(this);
    QIcon icon(":/pic/rss.png");
    setWindowIcon(icon);
    currentnewsnum=0;

    connect(nextButton,SIGNAL(clicked()),this,SLOT(nextNews()));
    connect(previousButton,SIGNAL(clicked()),this,SLOT(previousNews()));
    connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));
    connect(openBrowserButton,SIGNAL(clicked()),this,SLOT(openBrowser()));
    connect(settingButton,SIGNAL(clicked()),this->newssetting,SLOT(show()));
}

void NewsViewer::readFile(){
    using namespace std;
    ifstream fin("D:/lecture.txt");
    fin>>newsnum;
    string s;
    getline(fin,s);
    for(int i=0;i<newsnum;i++)
    {
        getline(fin,s);
        newslink[i]=QString::fromStdString(s);
        getline(fin,s);
        newscontent[i]=QString::fromStdString(s);
    }

    newsLabel->setText(this->newscontent[currentnewsnum]);
}

void NewsViewer::nextNews()
{
    if(currentnewsnum==newsnum-1)
        currentnewsnum=0;
    else
        currentnewsnum++;

    newsLabel->setText(this->newscontent[currentnewsnum]);
}

void NewsViewer::previousNews()
{
    if(currentnewsnum==0)
        currentnewsnum=newsnum-1;
    else
        currentnewsnum--;

    newsLabel->setText(this->newscontent[currentnewsnum]);
}

void NewsViewer::openBrowser()
{
    BrowserWindow *browserwindow = new BrowserWindow(newslink[currentnewsnum]);
    browserwindow->show();
}

void NewsViewer::settingDialogShow(){
    newssetting->show();
}
